<template>
  <el-dialog :title="title" :visible.sync="dialogShow" @close="closeDialog">
    <el-form
      :model="formData"
      :rules="formRules"
      ref="form"
      label-width="160px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 440px"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 440px"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 440px"
          @focus="getEmployeeData"
        >
          <el-option
            v-for="(item, index) in managerList"
            :key="index"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 440px"
          rows="3"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  getDepartmentListApi,
  addDepartmentListApi,
  getDepartmentInfoApi,
  editDepartmentListApi
} from '@/api/departments'
import { getEmployeeSimpleApi } from '@/api/employees'
export default {
  name: 'AddDept',
  data() {
    const checkNameRepeat = async (vaild, value, cb) => {
      let { depts } = await getDepartmentListApi()
      let arr = depts.filter((item) => item.pid === this.formData.pid)
      console.log(arr)
      if (this.formData.id) {
        arr = arr.filter((item) => item.id !== this.formData.id)
      }
      const isRepeat = arr.some((item) => item.name === value)
      isRepeat ? cb(new Error('名称重复了')) : cb()
    }
    const checkCodeRepeat = async (vaild, value, cb) => {
      let { depts } = await getDepartmentListApi()
      console.log(depts)
      if (this.formData.id) {
        depts = depts.filter((item) => item.id !== this.formData.id)
      }
      const isRepeat = depts.some((item) => item.code === value)
      isRepeat ? cb(new Error('编码重复了')) : cb()
    }
    return {
      dialogShow: false,
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      managerList: [],
      formRules: {
        code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '请输入', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '新增'
    }
  },
  methods: {
    async btnOk() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        console.log(error)
      }

      if (this.formData.id) {
        await editDepartmentListApi(this.formData)
        this.$message.success('修改成功')
      } else {
        await addDepartmentListApi(this.formData)
        this.$message.success('添加成功')
      }

      this.$emit('updateList')
      this.dialogShow = false
    },
    async getEmployeeData() {
      this.managerList = await getEmployeeSimpleApi()
    },
    closeDialog() {
      // this.dialogShow = false
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
      this.$refs.form.clearValidate()
    },
    async getFormDataInfo(id) {
      const res = await getDepartmentInfoApi(id)
      this.formData = res
      this.dialogShow = true
    }
  }
}
</script>

<style></style>
